Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clients/horizonclient: allow Fund on other networks and rely on server to indicate support #3891

Merged
merged 5 commits into from
Sep 7, 2021
Merged

clients/horizonclient: allow Fund on other networks and rely on server to indicate support #3891

merged 5 commits into from
Sep 7, 2021

Conversation

leighmcculloch
Copy link
Member

PR Checklist

PR Structure

  • This PR has reasonably narrow scope (if not, break it down into smaller PRs).
  • This PR avoids mixing refactoring changes with feature changes (split into two PRs
    otherwise).
  • This PR's title starts with name of package that is most changed in the PR, ex.
    services/friendbot, or all or doc if the changes are broad or impact many
    packages.

Thoroughness

  • This PR adds tests for the most critical parts of the new functionality or fixes.
  • I've updated any docs (developer docs, .md
    files, etc... affected by this change). Take a look in the docs folder for a given service,
    like this one.

Release planning

  • I've updated the relevant CHANGELOG (here for Horizon) if
    needed with deprecations, added features, breaking changes, and DB schema changes.
  • I've decided if this PR requires a new major/minor version according to
    semver, or if it's mainly a patch change. The PR is targeted at the next
    release branch if it's not a patch change.

What

Allow the Fund function to submit a fund request to any horizon instance, not just the instance defined by horizonclient.DefaultTestNetClient, and rely on the horizon instance responding with a 404 as an indicator that funding is not supported.

Why

The only way to construct a horizonclient.Client that supports the Fund operation is to use and mutate the horizonclient.DefaultTestNetClient. This is unnecessarily restrictive because a developer may wish to use the Fund operation with a non-default client that they have constructed themselves, or they may wish to use the Fund operation with their own private or standalone test network.

Known limitations

N/A

@leighmcculloch leighmcculloch requested review from ire-and-curses and a team and removed request for ire-and-curses September 5, 2021 03:11
Copy link
Contributor

@Shaptic Shaptic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good change, but have some musings below (and a typo fix).

clients/horizonclient/client.go Outdated Show resolved Hide resolved
).ReturnString(404, friendbotFundResponse)

_, err := client.Fund("GBLPP2W3X3PJQXYMC7EFWM5G2QCZL7HTCTFNMONS4ITGAYJ3GNNZIQ4V")
assert.EqualError(t, err, "funding is only available on test networks and may not supported by https://localhost/: horizon error: \"Resource Missing\" - check horizon.Error.Problem for more information")
Copy link
Contributor

@Shaptic Shaptic Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more of a philosophical discussion, but I've been meaning to ask this for a while: why do we test for specific error messages like this rather than just assert.Error, or maybe even a regex match for a substring on the error? I've read that the latter is considered an anti-pattern, but don't fully understand why.

My rationale against it:

  • awkward hard-coding of errors: we can't use constants, and so we have long strings here
  • increased test verbosity without (in my opinion) added value. For example, the message has a typo (see above), but this test wouldn't have caught that.
  • discourages changing error messages that may increase clarity or information density, since there are many places to edit it

Again, I get that this is more of a philosophical best-practices discussion (and one that shouldn't impede merging this PR ✔️), but this seemed like an isolated-enough PR to get myself some clarity on that.

Copy link
Member Author

@leighmcculloch leighmcculloch Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the distinction here is that this error string, that is wrapping the not found error in, is just an annotation or a hint, like a comment about the error. The error is still the Horizon not found error in the propoer horizon client error object. Because of this I don't it is worth adding a new error type for the annotation.

In regards to more generally, the reason that regexing isn't preferred is it is brittle. The preferred approach for error matching is to use errors.Is or errors.As, which works great with custom error types that wrap an error, or error constants that don't wrap another error.

In this specific case we can't use a constant for the error, otherwise we'd lose the underlying horizon not found error, which I think is actually the most important error in this scenario. In this specific case I don't think it is worth defining an error type to wrap the not found error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood your question.

why do we test for specific error messages like this rather than just assert.Error

assert.Error would tell us that any error occurred. Any error could occur for a variety of other reasons and we wouldn't necessarily be testing the exact scenario we intend.

assert.EqualError is maybe a little stricter than we need, but it works?

clients/horizonclient/client_fund_test.go Show resolved Hide resolved
@Shaptic Shaptic requested a review from a team September 7, 2021 20:24
@leighmcculloch leighmcculloch merged commit ea9fc73 into stellar:master Sep 7, 2021
@leighmcculloch leighmcculloch deleted the horizonclientfundothernetworks branch September 7, 2021 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants